/*toolSM stateMachine
Qiang.DAI
2025.08.30
YYYY.MM.DD
*/

#include "toolSM.h"

#include "main.h"



static s_StateTransform_t *SerchStateEventInTab(s_Statemachine_t *tmpSM_ptr, const SmEvent_t tmpTriggerEvent);



/**********************
@Function Name: SerchStateEventInTab
@Parameter:
*tmpSM_ptr: 		 the specific Peripheral's StateMachine Val
tmpTriggerEvent: TruggerEvent
@Return:
NULL:	not fit in tab
*ptr: the element, in trans Tab *ptr
*************************/
static s_StateTransform_t *SerchStateEventInTab(s_Statemachine_t *tmpSM_ptr, SmEvent_t tmpTriggerEvent)
{
    uint16_t i;
    for(i = 0; i < tmpSM_ptr->mStateQty; i++)
    {
        /*Secrch the Tab*/
        if((tmpSM_ptr->k_trans_ptr[i].mCurrentState == tmpSM_ptr->mState) && (tmpSM_ptr->k_trans_ptr[i].mEventTrigger == tmpTriggerEvent))
        {
            /*return TransTab element ptr*/
            return &tmpSM_ptr->k_trans_ptr[i];
        }
    }
    return NULL;
}

/**********************
@Function Name: runStateMachine
@Parameter:
*tmpSM_ptr: 		 the specific Peripheral's StateMachine Val
tmpTriggerEvent: TruggerEvent
@Return:
NULL:	none
*************************/
void RunStatemchine(s_Statemachine_t *tmpSM_ptr, SmEvent_t tmpTriggerEvent)
{

    s_StateTransform_t *tmpSMTrans_ptr;
    tmpSMTrans_ptr = 	SerchStateEventInTab(tmpSM_ptr, tmpTriggerEvent);
    if(tmpSMTrans_ptr == NULL)
    {
        return;			/*nothing find in transTab*/
    }
    /*update new state*/
    tmpSM_ptr->mState = tmpSMTrans_ptr->mNextState;
    /*get CallBack function ptr*/
    Action_t tmpActionFunktion = tmpSMTrans_ptr->actionMethod;
    /*run callback function*/
    tmpActionFunktion(&tmpTriggerEvent);
}
